<?php

namespace apiend\modules\rbac\models;

use Yii;
use yii\helpers\ArrayHelper;
use common\helpers\StringHelper;

/**
 * AssignmentSearch represents the model behind the search form about Assignment.
 * 
 * @author Misbahul D Munir <misbahuldmunir@gmail.com>
 * @since 1.0
 */
class AdminerSearch extends \common\models\admin\AdminUser {

    /**
     * @var string 关键字
     */
    public $keyword;

    /**
     * @var string 关键字
     */
    public $roleIdList;

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['keyword', 'roleIdList'], 'safe'],
        ];
    }

    /**
     * 查询检索
     * @inheritdoc
     */
    public function buildQuery() {
        $query = static::find()->active();
        if ($this->keyword) {
            $query->andFilterWhere([
                'or',
                ['like', 'username', $this->keyword],
                ['like', 'mobile', $this->keyword],
                ['like', 'profile', $this->keyword],
                ['like', 'realname', $this->keyword],
            ]);
        }
        $manager = Yii::$app->getAuthManager();
        $roles = $manager->getRolesByUser($this->id);
        return $query;
    }

    /**
     * @inheritdoc
     */
    public function fields() {
        return [
            'id',
            'name' => 'username',
            'mobile' => 'maskMobile',
            'avatar' => 'portrait',
            'introduction' => 'profile',
            'company',
            'status',
            'roles',
            'storeId',
            'created_at',
        ];
    }

}
